package view.items;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import model.Call;
import model.EnumCallStatus;
import model.Model;
import controller.Controller;

public class PanelBottomInfoCall extends JPanel {


	private Controller controller;
	private Model model;
	private PanelSlider slider;
	private JLabel volumeSlider;

	public PanelBottomInfoCall(Controller controller){
		this.setBackground(Color.orange);
		this.controller = controller;
		this.model=controller.getModel();
		this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 5));
	}

	public void refresh() {
		this.removeAll();
		this.revalidate();
		Call call = this.model.getSelectedCall();

		if(call !=null){
			
			JLabel labelCallSelected = new JLabel("Identity : "+call.getLastName()+" "+call.getFirstName());
			labelCallSelected.setPreferredSize(new Dimension(235, 15));
			this.add(labelCallSelected);

			JLabel ipCall = new JLabel("Ip : "+call.getIp().toString().replace("/", ""));
			ipCall.setPreferredSize(new Dimension(235, 20));
			this.add(ipCall);

			JLabel labelStatus = new JLabel("Call status : "+call.getStatus());
			labelStatus.setPreferredSize(new Dimension(235, 15));
			this.add(labelStatus);

			JLabel udpinfo = new JLabel("Udp Local/Distant : "+call.getLocalUdpPort()+"/"+call.getDistUdpPort());
			udpinfo.setPreferredSize(new Dimension(235, 15));
			this.add(udpinfo);
			
			JPanel blank = new JPanel();
			blank.setBackground(Color.orange);
			blank.setPreferredSize(new Dimension(470, 5));
			this.add(blank);
			
//			JLabel soundInfo = new JLabel("Sound info : "+call.getAudioFormat().toString());
//			this.add(soundInfo);

			if(call.getStatus() == EnumCallStatus.ACTIV_ESTABLISHED){

				ImageIcon muteSpeaker;
				JLabel muteSpeakerLabel = new JLabel();
				
				if(call.getMuteSpeaker()){
					muteSpeaker = new ImageIcon(getClass().getResource("/img/speakerOff.png"));
					muteSpeakerLabel = new JLabel("Speaker : Off");
				muteSpeakerLabel.setPreferredSize(new Dimension(95, 40));}
				else{
					muteSpeaker = new ImageIcon(getClass().getResource("/img/speakerOn.png"));
					muteSpeakerLabel = new JLabel("Speaker : On");
					muteSpeakerLabel.setPreferredSize(new Dimension(95, 40));
				}
				JButton buttonMuteSpeaker = new JButton(muteSpeaker);
				buttonMuteSpeaker.setContentAreaFilled(false);
				buttonMuteSpeaker.setBorderPainted(false);
				buttonMuteSpeaker.setActionCommand("buttonMuteSpeaker");
				buttonMuteSpeaker.addActionListener(this.controller);
				buttonMuteSpeaker.setToolTipText("Click to mute/unmute speaker");
				this.add(muteSpeakerLabel);
				this.add(buttonMuteSpeaker);




				ImageIcon muteMicrophone;
				JLabel muteMicrophoneLabel=new JLabel();
				
				if(call.getMuteMicrophone()){
					muteMicrophone = new ImageIcon(getClass().getResource("/img/microOff.png"));
					muteMicrophoneLabel = new JLabel("Microphone : Off");
					muteMicrophoneLabel.setPreferredSize(new Dimension(120, 40));
				}
				else{
					muteMicrophone = new ImageIcon(getClass().getResource("/img/microOn.png"));
					muteMicrophoneLabel = new JLabel("Microphone : On");
					muteMicrophoneLabel.setPreferredSize(new Dimension(120, 40));
				}
				JButton buttonMuteMicro = new JButton(muteMicrophone);
				buttonMuteMicro.setContentAreaFilled(false);
				buttonMuteMicro.setBorderPainted(false);
				buttonMuteMicro.setActionCommand("buttonMuteMicro");
				buttonMuteMicro.addActionListener(this.controller);
				buttonMuteMicro.setToolTipText("Click to mute/unmute microphone");
				this.add(muteMicrophoneLabel);
				this.add(buttonMuteMicro);



				slider = new PanelSlider(controller);
				this.add(slider);
				volumeSlider = new JLabel(String.valueOf(this.controller.getModel().getSelectedCall().getVoiceActivationLevel()));
				this.add(volumeSlider);
			}
			
			
		}
		else{
			JLabel labelNone = new JLabel("Select a Call to have information");
			this.add(labelNone);
		}
		this.revalidate();
	}

	public PanelSlider getSlider() {
		return slider;
	}

	public JLabel getVolumeSlider() {
		return volumeSlider;
	}


}
